package com.hhy.view;


import com.hhy.entity.User;
import com.hhy.service.UserService;
import com.hhy.service.impl.UserServiceImplement;
import com.hhy.utils.IsNumer;
import com.hhy.utils.NumBerPool;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @Description 首页，有三个功能，一个是登录，一个是注册一个是退出系统
 * @Author hhy
 * @Date 2020/10/29 9:17
 */
public class Index {
    Scanner sc = new Scanner(System.in);

    UserService userService = new UserServiceImplement();

    public void init() {
        System.out.println("欢迎来到湖南省移动营业厅！！！");
        System.out.println("请选择一项操作：");
        System.out.println("1.登录");
        System.out.println("2.注册");
        System.out.println("3.退出系统");
        String num = "";
        num = sc.next();

        switch (num) {
            case "1":
                login();
                break;
            case "2":
                reg();
                break;
            case "3":
                System.out.println("谢谢使用!!!");
                System.exit(0);
            default:
                System.out.println("请重新输入正确的选项");
                init();
        }
    }

    //用户首页
    public void userIndex(User user) {
        System.out.println("你好呀！" + user.getUsername() + "欢迎来到用户界面");
        System.out.println("现在有以下的功能可选");
        System.out.println("1)通话");
        System.out.println("2)充值");
        System.out.println("3)打印个人账户");
        System.out.println("4)销户");
        System.out.println("5)返回上一级");
        String num = sc.next();
        switch (num) {
            case "1":
                System.out.println("通话");
                System.out.println(userService.userCell(user));
                userIndex(user);
                break;
            case "2":
                System.out.println("充值");
                Double aDouble = addMola(user);
                System.out.println("当前还剩余"+aDouble);
                userIndex(user);
                break;
            case "3":
                userService.generateExcel(user);
                System.out.println("打印个人的账户完成");
                userIndex(user);
                break;
            case "4":
                System.out.println("销户");
                //销户成功的话，那就返回到主界面
                if (deleteUser(user)){
                    System.out.println("销户成功！！！");
                    init();
                }else {
                    System.out.println("销户失败！！！");
                    userIndex(user);
                }
                break;
            case "5":
                System.out.println("返回上一级");
                init();
                break;
            default:
                System.out.println("请输入正确的选项");
                userIndex(user);
        }

    }

    //管理员界面
    public void adminIndex(User user) {
        System.out.println("你好管理员！请选择你要的操作");
        System.out.println("1）修改用户密码");
        System.out.println("2）添加号码");
        System.out.println("3）返回上一级");
        String num = sc.next();
        switch (num) {
            case "1":
                modifyPassword();
                adminIndex(user);
                break;
            case "2":
                System.out.println("添加号码");
                Long newPhoneNumer = NumBerPool.createNewPhoneNumer();
                System.out.println("添加了一个新号码"+newPhoneNumer);
                break;
            case "3":
                System.out.println("返回上一级");
                init();
                break;
            default:
                System.out.println("请重新输入正确的选项");
                adminIndex(user);
        }

    }


    //注册
    private void reg() {
        System.out.println("欢迎来到注册页面，请输入一个你喜欢的昵称");
        String username = sc.next();
        System.out.println("你好！" + username + "下面有一些号码，请选择一个你喜欢的");
        List<Long> phoneNums = NumBerPool.numberList();

        for (int i = 0; i < phoneNums.size(); i++) {
            System.out.println(i + "）" + phoneNums.get(i));
        }
        String phoneNumberId = sc.next();
        Long phoneNumber = null;


        if (IsNumer.isNumber(phoneNumberId, phoneNums.size())) {
            phoneNumber = phoneNums.get(Integer.valueOf(phoneNumberId));
        } else {
            do {
                System.out.println("不喜欢这组号码吗，那就再换一组吧QAQ");
                phoneNums = NumBerPool.numberList();
                for (int i = 0; i < phoneNums.size(); i++) {
                    System.out.println(i + "）" + phoneNums.get(i));
                }
                phoneNumberId = sc.next();

            } while (!IsNumer.isNumber(phoneNumberId, phoneNums.size()));
            phoneNumber = phoneNums.get(Integer.valueOf(phoneNumberId));
        }

        System.out.println(username + "你选择的号码是" + phoneNumber);

        System.out.println("亲爱的" + username + "请输入你的密码");
        String password = sc.next();

        System.out.println("密码输入完成！");
        System.out.println("请输入您的号码" + phoneNumber + "需要充值的钱qwq");
        String mola = sc.next();

        //如果输入的又不是数字
        while (!IsNumer.isNumber(mola)) {
            System.out.println("呐呐呐！请输入数字呢");
            mola = sc.next();
        }



        System.out.println("亲爱的" + username + "您的号码" + phoneNumber + "现在的余额为" + mola);

        System.out.println("请选择您的套餐,目前套餐有两种");
        System.out.println("1) 3元套餐 \t" +
                "  5分钟免费通话时长，超过部分1.5元/分钟");
        System.out.println("2) 15元套餐 \t" +
                "  35分钟免费通话时长，超过部分0.3元/分钟");

        String meal = sc.next();
        while (!IsNumer.isNumber(meal, 2)) {
            System.out.println("呐呐呐！请选择正确的套餐呢");
            meal = sc.next();
        }
        //如果过了判断，就说明一定可以转换成数字，并且一定为1或者2
        meal = Integer.valueOf(meal) == 1 ? "3元套餐 + 5分钟免费通话时长，超过部分1.5元/分钟" : "15元套餐 " + "35分钟免费通话时长，超过部分0.3元/分钟";


        System.out.println("亲爱的" + username + "您的号码" + phoneNumber + "\n现在的余额为" + mola + "\n选择的套餐为" + meal);

        User user = new User(username, password, phoneNumber, meal);
        user.setCellTime(0);
        user.setCallCast(10.0);
        userService.registered(user);
        //最后是直接返回首页
        init();
    }

    //登录
    private void login() {
        System.out.println("欢迎来到移动登录页面！！！");
        System.out.println("请输入您的手机号码");
        String phoneNumber = sc.next();
        while (!IsNumer.isNumber(phoneNumber, true)) {
            System.out.println("手机号格式不对，请重新输入");
            phoneNumber = sc.next();
        }

        System.out.println("请输入您的密码");
        String password = sc.next();
        Long phone = Long.valueOf(phoneNumber);
        if (userService.login(phone, password)) {
            System.out.println("登录成功！！");
            User user = userService.getUserByPhoneNumber(phone);
            if (user.getAdmin()) {
                adminIndex(user);
            } else {
                userIndex(user);
            }
        } else {
            System.out.println("登录失败");
            init();
        }
    }

    //管理员修改密码
    private void modifyPassword() {
        System.out.println("请输入用户的手机号码");
        String phone = sc.next();
        while (!IsNumer.isNumber(phone, true)) {
            System.out.println("手机号码格式不对，请重新输入");
            phone = sc.next();
        }
        System.out.println("请输入用户的新密码");
        String password = sc.next();
        boolean modifyPassword = userService.modifyPassword(Long.valueOf(phone), password);
        if (modifyPassword) {
            System.out.println("密码修改成功！");
        } else {
            System.out.println("密码修改失败！");
        }
    }

    //销户
    private boolean deleteUser(User user){
        System.out.println("你确定要销户吗");
        String flag = sc.next();
        if (flag.equals("y")||flag.equals("yes")){
            return userService.deleteUser(user.getPhoneNumber());
        }

        return false;
    }

    //充值
    private Double addMola(User user){
        System.out.println("请输入你要充值的钱数");
        String mola = sc.next();
        return userService.addMola(user.getPhoneNumber(),Double.valueOf(mola));
    }
}
